package stringOperations;

public class RecursivelyRemoveAdjacentDuplicateStrings {
	public void removeDuplicates(StringBuilder s){
		int fp = 0;
		int mp = 1;
		int mps = 1;
		int len =0;
		while(len<s.length()){
			if(s.charAt(fp)==s.charAt(mp)){
				while(mps!=s.length()&&s.charAt(mps)==s.charAt(mp))
					mps++;
				
				remove(s,fp,mps);
				fp=0;
				mp=fp+1;
				mps= mp;
			}
			else if(s.charAt(fp)!=s.charAt(mp)){
				removeDuplicates(new StringBuilder(s.substring(mp)));
				if (s.charAt(fp)==s.charAt(mp)){
					s.deleteCharAt(fp); s.deleteCharAt(mp);
				}
			}
			
		}
		
	}
	public void remove(StringBuilder s, int fp, int mps){
		for(int i=fp;i<mps;i++)
		s.deleteCharAt(fp);
	}
	public static void main(String[] args) {
		StringBuilder s=  new StringBuilder("aaabcddcebeefbf");
		new RecursivelyRemoveAdjacentDuplicateStrings().removeDuplicates(s);
		System.out.println("new Striing "+ s);
	}

}
